﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.AppAuth
{
    [CPPEI.WebApi.AuthGetAction("AppAuth", "GetList", "1.0")]
    public class GetListAction : BaseAction<GetListParams, GetListResult>
    {
        protected override ActionResult<GetListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetListParams opParams)
        {
            SER.AppAuthorizationService service = new SER.AppAuthorizationService();
            PagedList<ENT.EAppAuthorization> serResult = service.GetList(opParams.Filter, opParams.Page * 25, 25);

            if(serResult.IsSuccess)
            {
                GetListResult result = new GetListResult();

                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Total = serResult.Total;
                result.Page = opParams.Page;

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetListResult.Item item = new GetListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.AppId = serResult.Data[index].AppId;
                    item.AppName = serResult.Data[index].AppName;
                    //item.Description = serResult.Data[index].Description;
                    item.Token = serResult.Data[index].Token;
                    item.CreatedTime = serResult.Data[index].CreatedTime;
                    item.IsEnabled = serResult.Data[index].IsEnabled;
                    result.Items.Add(item);
                }

                return new ActionResult<GetListResult>(result);
            }
            else
            {
                return new ActionResult<GetListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetListParams
    {
        public int Page { get; set; }

        public string Filter { get; set; }
    }

    public class GetListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public Guid AppId { get; set; }

            public string AppName { get; set; }

            public string Token { get; set; }

            //public string Description { get; set; }

            public DateTime CreatedTime { get; set; }
            public bool IsEnabled { get; set; }
        }
    }
}
